package com.mmy.controller.line;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.line.Place;
import com.mmy.domain.line.TLineBase;
import com.mmy.domain.line.TLineOrder;
import com.mmy.domain.line.TLinePlace;
import com.mmy.domain.line.TLineTrip;
import com.mmy.domain.line.VLineBase;
import com.mmy.domain.line.VLineOrder;
import com.mmy.domain.line.VLinePlan;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.line.LineBaseServiceImpl;
import com.mmy.service.impl.line.LineOrderServiceImpl;
import com.mmy.service.impl.line.LinePlaceServiceImpl;
import com.mmy.service.impl.line.LinePlanServiceImpl;
import com.mmy.service.impl.line.LineTripServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;

@Controller
public class LineBaseController {

	@Autowired
	LineBaseServiceImpl lineBaseServiceImpl;
	@Autowired
	LineTripServiceImpl lineTripServiceImpl;
	@Autowired
	LineOrderServiceImpl lineOrderServiceImpl;
	@Autowired
	LinePlanServiceImpl linePlanServiceImpl;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	LinePlaceServiceImpl linePlaceServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转--线路列表主页
	 *
	 * @param 参数说明 vLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人liys
	 * @date 创建日期 2020.08.18  修改时间2021.03.23
	 */

	@RequestMapping("/surroudingtourismindex")
	public String surroudingTourismIndex(HttpServletRequest request,String type,HttpSession session,Model model) {
		
		Integer iType = Integer.parseInt(type);
		model.addAttribute("type",iType);
		//地区
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("lstConstZone", lstConstZone);
		List<TConstZone> lstConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("lstConstZoneDistrict", lstConstZoneDistrict);
		//登录人
		List<TSysOrg> lstSysOrg = sysOrgServiceImpl.findAll();
		model.addAttribute("lstSysOrg", lstSysOrg);
		//下拉框
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		Integer iOrgID = tSysUser.getOrgID();
		List<VLineBase> lstLineBase = lineBaseServiceImpl.findSurrouding(iOrgID,iType);
		model.addAttribute("lstLineBase",lstLineBase);
		return "webbackground/tourist/TourismIndex";
	}
	/**
	 * 函数功能说明： 跳转--新建线路页
	 *
	 * @param 参数说明 
	 * @return 返回值说明 jsp
	 * @author 创建人 hanxl 修改人 liys
	 * @date 创建日期 2020.08.18 修改日期 2021.01.22
	 */

	@RequestMapping("/tocreatesurrouding")
	public String toCreateSurrouding(Model model, HttpSession session,HttpServletRequest request) {
		
		Integer iType = Integer.parseInt(request.getParameter("type"));
		model.addAttribute("type",iType);
		// 查询所有洲
		List<TConstZone> lstZoneContinent = constZoneServiceImpl.findAllContinent();
		model.addAttribute("lstZoneContinent", lstZoneContinent);
		//省
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<TLinePlace> lstLinePlace = linePlaceServiceImpl.findByOrgID(tSysUser.getOrgID());
		model.addAttribute("lstLinePlace", lstLinePlace);
		return "webbackground/tourist/CreateSurrouding";
	}

	/**
	 * 函数功能说明：提交--新建的线路
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp
	 * @author 创建人 liys
	 * @date 创建日期 2021.01.22
	 */
	@RequestMapping("/savecreatesurrouding")
	public String saveCreateSurrouding(@RequestParam("base") String strJsonBase,@RequestParam("trip") String strJsonTrip, 
			@RequestParam("place")String strJsonPlace,@RequestParam("type")String type,HttpSession session, HttpServletRequest request) {
		
		Integer iType = Integer.parseInt(type);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iLineID = common.createBaseCode(UtilConstants.TableName.T_LINE_BASE);
		List<TLineBase> lstLineBase = JSON.parseArray(strJsonBase,TLineBase.class);
		List<TLineTrip> lstLineTrip = JSON.parseArray(strJsonTrip, TLineTrip.class);
		List<Place> lstPlace = JSON.parseArray(strJsonPlace, Place.class);
		for(TLineBase tLineBase : lstLineBase) {
			tLineBase.setLineID(iLineID);
			tLineBase.setCreateDate(new Date());
			tLineBase.setOrgID(tSysUser.getOrgID());
			tLineBase.setCreator(tSysUser.getUserName());
			tLineBase.setType((byte) 1);
			tLineBase.setDay(lstLineTrip.size());
			Map<String,Object> mapPlace = linePlaceServiceImpl.save(lstPlace,tSysUser);
			tLineBase.setPlaceID(mapPlace.get("cPlaceID").toString());
			tLineBase.setTime(mapPlace.get("cTime").toString());
			lineTripServiceImpl.save(lstLineTrip,tSysUser,iLineID);
			lineBaseServiceImpl.save(tLineBase.getLineID(),iType);
			lineBaseServiceImpl.save(tLineBase);
		}
		
		return "webbackground/tourist/TourismIndex";
	}
	/**
	 * 函数功能说明： 跳转--线路修改页
	 *
	 * @param 参数说明 iLineID
	 * @return 返回值说明 jsp
	 * @author 创建人 liys
	 * @date 创建日期 2020.12.22
	 */
	@RequestMapping("/toupdatelinebasetrip")
	public String toUpdateLineBaseTrip(HttpServletRequest request, Model model, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strLineID = request.getParameter("iLineID");
		TLineBase tLineBase = lineBaseServiceImpl.findByiLineID(Integer.parseInt(strLineID));
		List<Map<String,String>> lstPlaceTime = linePlaceServiceImpl.matchPlaceTime(tLineBase);
		model.addAttribute("lstPlaceTime",lstPlaceTime);
		model.addAttribute("tLineBase", tLineBase);
		
		int iType = tLineBase.getType();
		if(iType !=3) {
			List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
			model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
			Integer zoneID = tLineBase.getZoneID();
			List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(zoneID);
			model.addAttribute("lstConstZoneCity", lstConstZoneCity);
			List<TConstZone> lstConstZoneDistrict = constZoneServiceImpl.findDistricts(zoneID);
			model.addAttribute("lstConstZoneDistrict", lstConstZoneDistrict);
			Map<String,Object> province = constZoneServiceImpl.findProvinceMap(zoneID);
			Map<String,Object> city = constZoneServiceImpl.findCityMap(zoneID);
			Map<String,Object> district = constZoneServiceImpl.findDistrictMap(zoneID);
			model.addAttribute("province", province);
			model.addAttribute("city", city);
			model.addAttribute("district", district);
		}else {
			List<TConstZone> lstContinent = constZoneServiceImpl.findAllContinent();
			model.addAttribute("Continent",lstContinent);
		}
		//集合地
		List<TLinePlace> lstLinePlace = linePlaceServiceImpl.findByOrgID(tSysUser.getOrgID());
		model.addAttribute("lstLinePlace", lstLinePlace);
		//行程
		List<TLineTrip> lstLineTrip = lineTripServiceImpl.findByLineID(Integer.parseInt(strLineID));
		model.addAttribute("lstLineTrip", lstLineTrip);
		model.addAttribute("iLineID", Integer.parseInt(strLineID));
		return "webbackground/tourist/UpdateLineBaseTrip";
	}

	/**
	 * 函数功能说明： 提交--线路修改信息
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @date 创建日期 2020.12.22
	 */
	@RequestMapping("/saveupdatelinebasetrip")
	public String saveUpdateLineBaseTrip(@RequestParam(value = "base") String strJsonBase,@RequestParam("trip") String strJsonTrip,
			@RequestParam(value = "id") String strLineID,@RequestParam("place") String strJsonPlace,HttpSession session, HttpServletRequest request) {
		
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iLineID = Integer.parseInt(strLineID);
		TLineBase newLineBase = lineBaseServiceImpl.findByiLineID(iLineID);
		List<TLineBase> lstLineBase = JSON.parseArray(strJsonBase, TLineBase.class);
		List<Place> lstPlace = JSON.parseArray(strJsonPlace,Place.class);
		for(TLineBase tLineBase : lstLineBase) {
			newLineBase.setLineName(tLineBase.getLineName());
			newLineBase.setZoneID(tLineBase.getZoneID());
			newLineBase.setTime(tLineBase.getTime());
			//newLineBase.setItineraryPath(tLineBase.getItineraryPath());
			newLineBase.setOwn(tLineBase.getOwn());
			newLineBase.setTips(tLineBase.getTips());
			newLineBase.setExplain(tLineBase.getExplain());
			newLineBase.setStandard(tLineBase.getStandard());
			newLineBase.setMemo(tLineBase.getMemo());
			newLineBase.setModifyDate(new Date());
		}
		Map<String, Object> mapPlace = linePlaceServiceImpl.save(lstPlace,tSysUser);
		newLineBase.setPlaceID(mapPlace.get("cPlaceID").toString());
		newLineBase.setTime(mapPlace.get("cTime").toString());
		List<TLineTrip> lstLineTrip = JSON.parseArray(strJsonTrip, TLineTrip.class);
		lineTripServiceImpl.deleteByLineID(iLineID);
		lineTripServiceImpl.save(lstLineTrip,tSysUser,iLineID);
		newLineBase.setDay(lstLineTrip.size());
		lineBaseServiceImpl.save(newLineBase);
		return "redirect:tourismindex";
	}

	/**
	 * 函数功能说明： 跳转--线路复制页
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @date 创建日期 2020.12.23
	 */
	@RequestMapping("/tocopylinebasetrip")
	public String tocopyLineBaseTrip(HttpServletRequest request, Model model, HttpSession session) {

		String strLineID = request.getParameter("iLineID");
		TLineBase tLineBase = lineBaseServiceImpl.findByiLineID(Integer.parseInt(strLineID));
		model.addAttribute("tLineBase", tLineBase);
		List<Map<String,String>> lstPlaceTime = linePlaceServiceImpl.matchPlaceTime(tLineBase);
		model.addAttribute("lstPlaceTime",lstPlaceTime);
		//所有的省
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		Integer zoneID = tLineBase.getZoneID();
		//所在省的全部市
		List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(zoneID);
		model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		//所在市的全部区
		List<TConstZone> lstConstZoneDistrict = constZoneServiceImpl.findDistricts(zoneID);
		model.addAttribute("lstConstZoneDistrict", lstConstZoneDistrict);
		//原来的省市区
		Map<String,Object> province = constZoneServiceImpl.findProvinceMap(zoneID);
		Map<String,Object> city = constZoneServiceImpl.findCityMap(zoneID);
		Map<String,Object> district = constZoneServiceImpl.findDistrictMap(zoneID);
		model.addAttribute("province", province);
		model.addAttribute("city", city);
		model.addAttribute("district", district);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<TLinePlace> lstLinePlace = linePlaceServiceImpl.findByOrgID(tSysUser.getOrgID());
		model.addAttribute("lstLinePlace", lstLinePlace);
		VLineBase vLineBase = lineBaseServiceImpl.findByLineID(Integer.parseInt(strLineID));
		model.addAttribute("vLineBase", vLineBase);
		//行程
		List<TLineTrip> lstLineTrip = lineTripServiceImpl.findByLineID(Integer.parseInt(strLineID));
		model.addAttribute("lstLineTrip", lstLineTrip);
		model.addAttribute("iLineID", Integer.parseInt(strLineID));
		return "webbackground/tourist/CopyLineBaseTrip";
	}

	/**
	 * 函数功能说明： 提交--删除线路信息
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 json 删除成功的线路集合
	 * @author 创建人 hanxl 修改人liys
	 * @date 创建日期 2020.08.18 修改日期 2021.01.27
	 */
	@RequestMapping("/deletesurroudingchecked")
	@ResponseBody
	public String deleteSurroudingChecked(String checkList) {
		String[] arrLineID = checkList.split(",");
		List<Integer> lstLineID = new ArrayList<>();
		for (String strLineID : arrLineID) {
			Integer iLineID = Integer.parseInt(strLineID);
			List<VLineOrder> list = lineOrderServiceImpl.findByLineID(iLineID);
			if(list.size() == 0)
				lstLineID.add(Integer.parseInt(strLineID));
			else continue;
		}
		lineBaseServiceImpl.deleteChecked(lstLineID);
		lineTripServiceImpl.delByLineIdList(lstLineID);
		linePlanServiceImpl.delByLineIdList(lstLineID);
		
		return JSONObject.toJSONString(lstLineID);

	}

	/**
	 * 函数功能说明： 异步--线路列表数据
	 * @param 参数说明 vLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @date 创建日期 2021.02.09
	 *
	 */

	@RequestMapping("/surroudingJson")
	@ResponseBody
	public String surroudingJson(HttpServletRequest request,HttpSession session) {
		
		int iType = Integer.parseInt(request.getParameter("type"));
		//接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		//搜索条件
		String strLineName = request.getParameter("lineName");
		String strZoneID = request.getParameter("zoneID");
		String strCity = request.getParameter("city");
		String strProvince = request.getParameter("province");
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		Integer iOrgID = tSysUser.getOrgID();
		//表格数据
		List<VLineBase> lstLineBaseSurrouding = lineBaseServiceImpl.findSurroudingPageTest(iOrgID,strLineName,strZoneID,strCity,strProvince,startIndex, count,iType);
		for(VLineBase vlineBase : lstLineBaseSurrouding) {	
			Integer iZoneID =  vlineBase.getZoneID();
			Map<String,Object> province = constZoneServiceImpl.findProvinceMap(iZoneID);
			Map<String,Object> city = constZoneServiceImpl.findCityMap(iZoneID);
			vlineBase.setZoneName(province.get("cZoneName").toString() + city.get("cZoneName").toString() + vlineBase.getZoneName());
		}
		//数据总数量
		int total = lineBaseServiceImpl.queryBaseCount(iOrgID, strLineName, strZoneID, strCity, strProvince,iType);	
		Map<String,Object> ret = new HashMap<String,Object>();
		ret.put("rows", lstLineBaseSurrouding);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}
	/**
	 * 函数功能说明： 异步--近郊游线路名下拉框
	 * @param 参数说明
	 * @return 返回值说明 lineName
	 * @author 创建人 liys
	 * @date 创建日期 2021.02.09
	 *
	 */
	@RequestMapping("/surroudingName")
	@ResponseBody
	public String findLineName(HttpSession session,HttpServletRequest request) {
		
		int iType = Integer.parseInt(request.getParameter("type"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		Integer iOrgID = tSysUser.getOrgID();
		List<Map<String,Object>> lstLineName = lineBaseServiceImpl.getLineName(iOrgID,iType);
		return JSONObject.toJSONString(lstLineName);
	}
	/**
	 * 函数功能说明： 跳转--查看线路信息详情
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 修改人 liys
	 * @date 创建日期 2020.07.29 修改时间 2021.01.22
	 */

	@RequestMapping("/linebaseinfo")
	public String toLineBaseInfo(HttpServletRequest request, Model model) {

		int iLineID = Integer.parseInt(request.getParameter("LineID"));
		VLineBase vLineBase = lineBaseServiceImpl.findByLineID(iLineID);
		List<Map<String,String>> lstPlaceTime = linePlaceServiceImpl.matchPlaceTime(vLineBase);
		model.addAttribute("lstPlaceTime",lstPlaceTime);
		model.addAttribute("vLineBase", vLineBase);
		int iType = vLineBase.getType();
		model.addAttribute("type",iType);
		if(iType!=3) {
			String strProvince = constZoneServiceImpl.findProvince(vLineBase.getZoneID()).getZoneName();
			String strCity = constZoneServiceImpl.findCity(vLineBase.getZoneID()).getZoneName();
			model.addAttribute("strProvince", strProvince);
			model.addAttribute("strCity", strCity);
		}else {
			String strCountry = constZoneServiceImpl.findCountry(vLineBase.getZoneID()).getZoneName();
			model.addAttribute("strCountry",strCountry);
		}
		List<TLineTrip> lstLineTrip = lineTripServiceImpl.findByLineID(iLineID);
		model.addAttribute("lstLineTrip", lstLineTrip);
		return "webbackground/tourist/LineBaseInfo";
	}
	
	/**
	 * 函数功能说明：跳转--查看线路订单信息详情
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人liys
	 * @date 创建日期  2020.11.18 修改时间 2020.12.28
	 */
	@RequestMapping("/orderlineinfo")
	public String toOrderLineInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		String strOrderID = tLineOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(0, 8));
		VLinePlan vLinePlan = linePlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("vLinePlan", vLinePlan);
		return "webbackground/tourist/OrderLineInfo";
	}

	/**
	 * 函数功能说明：跳转--收款打印页
	 *
	 * @param 参数说明 iLineID,iID
	 * @return 返回值说明 jsp
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 修改时间 2021.01.27
	 */
	@RequestMapping("/printline")
	public String toPrintLine(HttpServletRequest request, Model model) {
		
		int iType = Integer.parseInt(request.getParameter("type"));
		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		List<VLineOrder> lstLineOrder = lineOrderServiceImpl.searchReserveGroup(tLineOrder.getReserveName(), tLineOrder.getReserveDate(),iType);
		model.addAttribute("tLineOrder", tLineOrder);
		model.addAttribute("lstLineOrder", lstLineOrder);
		return "webbackground/tourist/PrintLine";
	}

	/**
	 * 函数功能说明： 跳转--退款打印页
	 *
	 * @param 参数说明 tLineBase
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */

	@RequestMapping(value = "/printcancelline")
	public String toPrintCancelLine(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TLineOrder tLineOrder = lineOrderServiceImpl.findByID(iID);
		model.addAttribute("tLineOrder", tLineOrder);
		return "webbackground/tourist/PrintCancelLine";
	}
	/**
	  * @descr 本公司的线路--下拉框数据
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-17
	 */
	@RequestMapping("/getLinesByOrg")
	@ResponseBody
	public String getLinesByOrg(HttpSession session) {
		
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		int iOrgID = tSysUser.getOrgID();
		List<Map<String,Object>> res = lineBaseServiceImpl.getLinesByOrg(iOrgID);
		return JSONObject.toJSONString(res);
	}
}
